package oms.dao;

import java.util.List;

import oms.entities.Role;
import oms.entities.User;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class RoleDAOImpl implements RoleDAO {
	
	@Autowired
	private SessionFactory sessionFactory;

	@Override
	public Role getRoleById(int roleId) {
		
		 return (Role) sessionFactory.getCurrentSession().get(Role.class, roleId);
	}

	@Override
	public Role getRoleByName(String name) {
		
		
			Session session = sessionFactory.getCurrentSession();
			Criteria criteria = session.createCriteria(User.class).add(
					Restrictions.eq("name", name)
					);
			List<Role> roles = (List<Role>) criteria.list();

			if(roles!=null && roles.size()!=0) {
				return roles.get(0);
			}
			return null;
		
	}



}
